package com.zhsj.OkeHomeConsumption.ui.activity.bussiness;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.bar.TitleBar;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.empty.BusinessBean;
import com.zhsj.OkeHomeConsumption.empty.BusinessLookpayeeBean;
import com.zhsj.OkeHomeConsumption.empty.BussinessjinriBean;
import com.zhsj.OkeHomeConsumption.empty.CityLookpayeeBean;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.ui.activity.bussiness.mvp.BusinessLookpayeePresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.bussiness.mvp.BusinessLookpayeeUIinterface;
import com.zhsj.OkeHomeConsumption.utils.JsonUtils;
import com.zhsj.OkeHomeConsumption.utils.SharedPreferanceUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BussinessLookpayeeActivity extends MyActivity implements BusinessLookpayeeUIinterface {
    @BindView(R.id.businesspayee_title)
    public TitleBar businesspayee_title;
    @BindView(R.id.business_recy)
    public RecyclerView business_recy;
    private int userid;
    private FormalUserInfo userInfo;
    private BusinessLookpayeePresenter minePersonPresenter = null;
    private SharedPreferanceUtils mSharePreferanceUtils = null;
    private BusinessLookAdapter lookadapter = null;
    private CityLookpayeeAdapter mCityLookpayeeapter = null;
    private jinriAdapter jifenadap;
    private List<BusinessLookpayeeBean> businessBeans = null;

    private List<CityLookpayeeBean> CityLookpayee = null;

    private List<BussinessjinriBean> jinrishouru_list = null;
    private int flag;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bussiness_lookpayee;
    }

    @Override
    protected int getTitleId() {
        return R.id.businesspayee_title;
    }
    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        business_recy.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        userInfo = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        minePersonPresenter = new BusinessLookpayeePresenter(getActivity(),this);
        Intent intent = getIntent();
        //6 收入记录 7 总收入 8余额 9已提现
        flag = intent.getIntExtra("flag", 0);//收入记录

        switch (flag){
            case 6:
                businesspayee_title.setTitle("收入记录");
                minePersonPresenter.TotalrevenueGet(userInfo.getId(),1);
                break;
            case 7:
                businesspayee_title.setTitle("总收入记录");
                minePersonPresenter.BusinessTotalrevenueGet(userInfo.getId(),1);
                break;
            case 8:
                businesspayee_title.setTitle("预计收入");
                minePersonPresenter.BusinessBlanceGet(userInfo.getId(),1);
                break;
            case 9:
                businesspayee_title.setTitle("已提现");
                minePersonPresenter.BusinessExtractGet(userInfo.getId(),1);
                break;



        }

    }

//总收入记录
    @Override
    public void BusinessTotalrevenueGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsondata = jsonOBJ.optJSONObject("data");
                String totalIncome   = jsondata.optString("incomeAmount");//收款共计
                String incomeTotalCount    = jsondata.optString("incomeNum");//收款总笔数
                JSONArray totalIncomeList = jsondata.optJSONArray("totalIncomeList");//总收入  收入记录
                CityLookpayee = new Gson().fromJson(totalIncomeList.toString(), new
                            TypeToken<ArrayList<CityLookpayeeBean>>() {
                            }.getType());

                mCityLookpayeeapter = new CityLookpayeeAdapter();
                business_recy.setAdapter(mCityLookpayeeapter);
                View headerView = getHeaderView(incomeTotalCount,totalIncome,"","");
                mCityLookpayeeapter.addHeaderView(headerView);

            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            log("获取商家--- 收入记录");
        }
    }
//余额记录
    @Override
    public void BusinessBlanceGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsondata = jsonOBJ.optJSONObject("data");
                JSONArray totalIncomeList = jsondata.optJSONArray("totalIncomeList");//余额列表
                String blance   = jsondata.optString("balance");//余额
                String totalIncome   = jsondata.optString("totalIncome");//收款共计
                String incomeTotalCount    = jsondata.optString("incomeTotalCount");//收款总笔数
                    businessBeans = new Gson().fromJson(totalIncomeList.toString(), new
                            TypeToken<ArrayList<BusinessLookpayeeBean>>() {
                            }.getType());
                lookadapter = new BusinessLookAdapter();
                View headerView = getHeaderView(incomeTotalCount,totalIncome,blance,"");
                lookadapter.addHeaderView(headerView);
                business_recy.setAdapter(lookadapter);

            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            log("获取商家---余额记录");
        }
    }
//已提现
    @Override
    public void BusinessExtractGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsondata = jsonOBJ.optJSONObject("data");
                JSONArray totalIncomeList = jsondata.optJSONArray("totalIncomeList");//提现列表
                String extractCash   = jsondata.optString("extractCash");//提现


                String blance   = jsondata.optString("balance");//余额
                String totalIncome   = jsondata.optString("totalIncome");//收款共计
                String incomeTotalCount    = jsondata.optString("incomeTotalCount");//收款总笔数
                CityLookpayee = new Gson().fromJson(totalIncomeList.toString(), new
                            TypeToken<ArrayList<CityLookpayeeBean>>() {
                            }.getType());
                mCityLookpayeeapter = new CityLookpayeeAdapter();
                business_recy.setAdapter(mCityLookpayeeapter);
                View headerView = getHeaderView(incomeTotalCount,totalIncome,blance,extractCash);
                mCityLookpayeeapter.addHeaderView(headerView);

            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            log("获取商家---提现记录");
        }
    }

    @Override
    public void TotalrevenueGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsondata = jsonOBJ.optJSONObject("data");
                JSONArray jsonarr = jsondata.optJSONArray("todayPaymentRecord");
                String todayIncome = jsondata.optString("todaytIncome");
                // 数据对接正常
                jinrishouru_list = new Gson().fromJson(jsonarr.toString(), new
                        TypeToken<ArrayList<BussinessjinriBean>>() {
                        }.getType());

                        jifenadap = new jinriAdapter();
                business_recy.setAdapter(jifenadap);
                View headerView = getHeaderView(jinrishouru_list.size()+"",todayIncome,"","");
                jifenadap.addHeaderView(headerView);
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            log("获取商家首页--- 收入记录");
        }
    }

    public class  BusinessLookAdapter extends BaseQuickAdapter<BusinessLookpayeeBean, BaseViewHolder>{

        public BusinessLookAdapter() {
            super(R.layout.item_lookpayee_layout, businessBeans);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, BusinessLookpayeeBean item) {
            helper.setText(R.id.name_text, item.getPayerUserName())
                    .setText(R.id.timer_text, String.valueOf(item.getCreateTime()))
                    .setText(R.id.price_text, "+" + item.getAmount());
        }
    }

    public class  CityLookpayeeAdapter extends BaseQuickAdapter<CityLookpayeeBean, BaseViewHolder>{

        public CityLookpayeeAdapter() {
            super(R.layout.item_lookpayee_layout, CityLookpayee);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, CityLookpayeeBean item) {
            helper.setText(R.id.name_text, item.getPayerUserName())
                    .setText(R.id.timer_text, String.valueOf(item.getCreateTime()))
                    .setText(R.id.price_text, "+" + item.getAmount());
        }
    }

    public class  jinriAdapter extends BaseQuickAdapter<BussinessjinriBean, BaseViewHolder>{

        public jinriAdapter() {
            super(R.layout.item_lookpayee_layout, jinrishouru_list);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, BussinessjinriBean item) {
            helper.setText(R.id.name_text, item.getPayerUserName())
                    .setText(R.id.timer_text, String.valueOf(item.getCreateTime()))
                    .setText(R.id.price_text, "+" + item.getAmount());
        }
    }

    private View getHeaderView(String incomeTotalCount, String totalIncome,String blance,String extractCash) {
        View view = getLayoutInflater().inflate(R.layout.item_lookpayeehead_layout, (ViewGroup) business_recy.getParent(), false);
        TextView shoukuan_text = view.findViewById(R.id.shoukuan_text);
        TextView price_text = view.findViewById(R.id.price_text);
//        ImageView image = view.findViewById(R.id.image);

        switch (flag){
            case 6:
                shoukuan_text.setText("收款笔数 :  " + incomeTotalCount);
                price_text.setText("收款共计  "+totalIncome);
                break;
            case 7:

                shoukuan_text.setText("收款笔数:   " + incomeTotalCount);
                price_text.setText("收款共计  "+totalIncome);
                break;
            case 8:
                shoukuan_text.setText("账户余额:    "+blance);
                price_text.setVisibility(View.GONE);
                break;
            case 9:
//                image.setImageResource(R.mipmap.tixian_img);
                shoukuan_text.setText("累积提现:    "+extractCash);
                price_text.setVisibility(View.GONE);
                break;
        }
        return  view;
    }
}
